﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleBlog.Infrastructure.Repositories;
using SimpleBlog.Models;
using SimpleBlog.Helpers;
using SimpleBlog.ViewModels;
using SimpleBlog.Mvc;
using System.Text.RegularExpressions;

namespace SimpleBlog.Controllers
{
    public class PostController : Controller
    {
        public static string SUMMARY_SEPARATOR = "<hr />";

        IPostRepository _postRepository;
        ITagRepository _tagRepository;

        public PostController(IPostRepository postRepository, ITagRepository tagRepository)
        {
            _postRepository = postRepository;
            _tagRepository = tagRepository;
        }

        public ActionResult Index(string name, int commentPage = 1, int commentCount = 5)
        {
            if (name == null)
                throw new HttpException(404, "You have to indicate a Post name.");

            var post = _postRepository.Search(x => x.EncodedTitle == name).FirstOrDefault();

            if (post == null)
                throw new HttpException(404, string.Format("The Post \"{0}\" doesn't exists.", name));
            
            return View(new PostViewModel()
            {
                PostItem = post,
                Summary = false,
                CommnetPage = commentPage,
                CommentCount = commentCount
            });
        }
    }
}
